/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on 28.11.2011, 16:46:08
 */
package client.ui;

import client.pipeConnector.IPipe;
import client.pipeConnector.PipeConnectorBuffered;
import client.ui.chatJPanelActivating.ChatJpanelActivatedProducer;
import protocolHandler.ProtoMessageCreator;
import java.io.UnsupportedEncodingException;

/**
 *
 * @author Александр Емельянов (mr.lex91@gmail.com)
 */
public class ChatJPanel extends javax.swing.JPanel {

    /** Creates new form NewJPanel */
    public ChatJPanel() {
        initComponents();
    }
    public ChatJPanel(IPipe pipeConnector, String messageHeader, String myNickName, ChatJpanelActivatedProducer actProd) {
        initComponents();
        this.pipeConnector=pipeConnector;
        this.messageHeader=messageHeader;
        this.myNickName=myNickName;
        this.actProd=actProd;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        jButton1.setText("Отправить");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Сообщение:");

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        jTextArea2.setColumns(20);
        jTextArea2.setFont(new java.awt.Font("Monospaced", 0, 11)); // NOI18N
        jTextArea2.setLineWrap(true);
        jTextArea2.setRows(5);
        jTextArea2.setDragEnabled(true);
        jTextArea2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextArea2MouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jTextArea2);

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setAutoscrolls(true);

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Monospaced", 0, 11)); // NOI18N
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextArea1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 435, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel1))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 540, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        actProd.activate(nickName);
        String text=jTextArea2.getText();
        if (text.length()!=0)
        {
            ProtoMessageCreator creator=new ProtoMessageCreator();
            creator.setType(messageHeader);
            creator.addHeader("Address", nickName);
            creator.addHeader("Size", String.valueOf(text.length()));
            creator.addHeader("Text", text);
            pipeConnector.Write(creator.Create());
            jTextArea2.setText("");
            Message(myNickName+": "+text);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextArea2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea2MouseClicked
        // TODO add your handling code here:
        actProd.activate(nickName);
    }//GEN-LAST:event_jTextArea2MouseClicked

    private void jTextArea1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTextArea1MouseClicked
        // TODO add your handling code here:
        actProd.activate(nickName);
    }//GEN-LAST:event_jTextArea1MouseClicked

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
        actProd.activate(nickName);
    }//GEN-LAST:event_formMouseClicked
//    public void setEncodingProperties(String encoding, boolean isUTF8Encoding)
//    {
//        this.encoding=encoding;
//        this.isUTF8Encoding=isUTF8Encoding;
//    }
    public void setMyNickName(String myNickName)
    {
        this.myNickName=myNickName;
    }
    @Override
    public void setEnabled(boolean enabled)
    {
        jButton1.setEnabled(enabled);
        jTextArea2.setEnabled(enabled);
    }
    public void Enable()
    {
        setEnabled(true);
    }
    public void Enable(String nickName)
    {
        this.nickName=nickName;
        setEnabled(true);
    }
    public void Disable()
    {
        setEnabled(false);
    }
    public void Message(String msg)
    {
        jTextArea1.append(((jTextArea1.getText().length()==0)?"":"\n\n")+msg);
        jTextArea1.setCaretPosition(jTextArea1.getText().length());
    }
    public void ConverseTo(String nickName)
    {
        String text=jTextArea2.getText();
        jTextArea2.setText(((text.length()==0)?"":text+" ")+nickName);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    // End of variables declaration//GEN-END:variables
    private IPipe pipeConnector;
    private String nickName;
    private String messageHeader;
    private String myNickName;
//    private boolean isUTF8Encoding;
//    private String encoding;
    private ChatJpanelActivatedProducer actProd;
}
